<?php
/**
 * 用户管理控制器
 */
namespace Home\Controller;
use Think\Controller;
class TjsjController extends AController {
    public function _empty(){
        $this->redirect('Home/index');
    }

    public function index(){
        $date = date('Y-m-d');
        $p = I('p');
        if(!$p){
            $p = 1;
        }
        $num = 10;
        //获取用户信息
        $user = M('user')->order('id desc')->page($p.','.$num)->select();
        $count      = M('user')->count();// 查询满足要求的总记录数
        foreach ($user as $key => $value) {
            $id = $user[$key]['id'];
            $today = M('info')->where('user_id = '.$id.'  and time >= "'.$date.'"')->field('user')->count();
            $total = M('info')->where('user_id = '.$id)->field('user')->count();
            $user[$key]['today'] = $today;
            $user[$key]['total'] = $total;
        }
        $todayStatis = M('info')->where('time >= "'.$date.'"')->count();
        $totalStatis = M('info')->count();
        import("ORG.Util.Page");// 导入分页类
        $Page = getpage($count,$num);
        if(LANG_SET != 'zh-cn'){
            $Page->setConfig('header','Total&nbsp;&nbsp;'.$count.'&nbsp;&nbsp;Record');
            $Page->setConfig('first','first');
            $Page->setConfig('prev','<<');
            $Page->setConfig('next','>>');
            $Page->setConfig('last','last');
        }
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('todayStatis',$todayStatis);
        $this->assign('totalStatis',$totalStatis);
        $this->assign('userStatis',$user);
        $this->display();
        //print_r($user);
    }

    public function JsonDataProcessing($json){
        $json = str_replace("&quot;","\"",$json);
        $json = json_decode($json,true);
        $text = iconv('utf-8','gb2312',$json['text']);
        return $text;
    }

    public function download_tjsj(){
        $uid = I('uid');
        if($uid){
            $tjsj = M('info')->where('user_id='.$uid)->order('time desc')->select();
        }else{
            $tjsj = M('info')->order('time desc')->select();
        }

        $time = date('Y-m-d');
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=$time-$uid-json.csv");

        $title =  "用户,姓名,电话,身份证号,性别,金额,微信,来源,贷款类型,具体产品,工作,具体工作,用途,房产,车,保单,公积金,社保,营业执照,流水,时间";
        $title =iconv('utf-8','gb2312',$title);
        echo $title."\n";

        foreach($tjsj as $key=>$value) {
            $user =iconv('utf-8','gb2312',$tjsj[$key]['user']);
            $name =iconv('utf-8','gb2312',$tjsj[$key]['name']);
            $tel =$tjsj[$key]['tel'];
            $IDcard =iconv('utf-8','gb2312',$tjsj[$key]['idcard']);
            $sex =iconv('utf-8','gb2312',$tjsj[$key]['sex']);
            $amount =$tjsj[$key]['amount'] || "noData";
            $weixin =$tjsj[$key]['weixin'];
            $from =iconv('utf-8','gb2312',$tjsj[$key]['from']);
            $type =iconv('utf-8','gb2312',$tjsj[$key]['type']);
            $product =iconv('utf-8','gb2312',$tjsj[$key]['product']);
            $work =iconv('utf-8','gb2312',$tjsj[$key]['work']);
            $workdetail =iconv('utf-8','gb2312',$tjsj[$key]['workdetail']);
            $use =iconv('utf-8','gb2312',$tjsj[$key]['use']);
            $house = $this->JsonDataProcessing($tjsj[$key]['house']);
            $car = $this->JsonDataProcessing($tjsj[$key]['car']);
            $policy = $this->JsonDataProcessing($tjsj[$key]['policy']);
            $fund = $this->JsonDataProcessing($tjsj[$key]['fund']);
            $social_security = $this->JsonDataProcessing($tjsj[$key]['social_security']);
            $license = $this->JsonDataProcessing($tjsj[$key]['license']);
            $warter = $this->JsonDataProcessing($tjsj[$key]['warter']);
            $time =$tjsj[$key]['time'];
            echo $user.','.$name.','.$tel.','.$IDcard.','.$sex.','.$amount.','.$weixin.','.$from.','.$type.','.$product.','.$work.','.$workdetail.','.$use.','.$house
                .','.$car.','.$policy.','.$fund.','.$social_security.','.$license.','.$warter.','.$time."\n";
        }
    }
}
